package user

import (
	"gitcode.com/G-YT/ANN/server/global"
	"gitcode.com/G-YT/ANN/server/model/common/response"
	"gitcode.com/G-YT/ANN/server/model/user"
	userReq "gitcode.com/G-YT/ANN/server/model/user/request"
	"gitcode.com/G-YT/ANN/server/service"
	"gitcode.com/G-YT/ANN/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strconv"
)

type UserBindManageApi struct {
}

var BindService = service.ServiceGroupApp.UserServiceGroup.UserBindManageService

// CreateUserBindManage 創建用户绑定健管师
// @Tags UserBindManage
// @Summary 創建用户绑定健管师
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body user.UserBindManage true "創建用户绑定健管师"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"創建成功"}"
// @Router /Bind/createUserBindManage [post]
func (BindApi *UserBindManageApi) CreateUserBindManage(c *gin.Context) {
	var Bind user.UserBindManage
	err := c.ShouldBindJSON(&Bind)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	id := utils.GetUserID(c)
	Bind.Uid = id
	if err := BindService.CreateUserBindManage(&Bind); err != nil {
		global.GVA_LOG.Error("提交失敗!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithMessage("提交成功", c)
	}
}

// GetBindUserList 獲取绑定用户列表
// @Tags UserBindManage
// @Summary 獲取绑定用户列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query userReq.UserBindManageSearch true "獲取绑定用户列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"獲取成功"}"
// @Router /Bind/getUserBindManageList [get]
func (BindApi *UserBindManageApi) GetBindUserList(c *gin.Context) {
	var pageInfo userReq.UserBindManageSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := BindService.GetBindUserInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage("獲取失敗", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "獲取成功", c)
	}
}

// UpdateBindStatus 更改绑定状态
// @Tags UserBindManage
// @Summary 更改绑定状态
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body user.UserBindManage true "更改绑定状态"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"操作成功"}"
// @Router /Bind/updateBindStatus [put]
func (BindApi *UserBindManageApi) UpdateBindStatus(c *gin.Context) {
	var Bind user.UserBindManage
	err := c.ShouldBindJSON(&Bind)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := BindService.UpdateBindStatus(Bind.ID, strconv.Itoa(int(Bind.Status))); err != nil {
		global.GVA_LOG.Error("操作失敗!", zap.Error(err))
		response.FailWithMessage("操作失敗", c)
	} else {
		response.OkWithMessage("操作成功", c)
	}
}

// DeleteUserBindManage 删除用户绑定健管师
// @Tags UserBindManage
// @Summary 删除用户绑定健管师
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body user.UserBindManage true "删除用户绑定健管师"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /Bind/deleteUserBindManage [delete]
func (BindApi *UserBindManageApi) DeleteUserBindManage(c *gin.Context) {
	id := c.Query("ID")
	if err := BindService.DeleteUserBindManage(id); err != nil {
		global.GVA_LOG.Error("删除失敗!", zap.Error(err))
		response.FailWithMessage("删除失敗", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteUserBindManageByIds 批量删除用户绑定健管师
// @Tags UserBindManage
// @Summary 批量删除用户绑定健管师
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除用户绑定健管师"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /Bind/deleteUserBindManageByIds [delete]
func (BindApi *UserBindManageApi) DeleteUserBindManageByIds(c *gin.Context) {
	ids := c.QueryArray("ids[]")
	if err := BindService.DeleteUserBindManageByIds(ids); err != nil {
		global.GVA_LOG.Error("批量删除失敗!", zap.Error(err))
		response.FailWithMessage("批量删除失敗", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateUserBindManage 更新用户绑定健管师
// @Tags UserBindManage
// @Summary 更新用户绑定健管师
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body user.UserBindManage true "更新用户绑定健管师"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /Bind/updateUserBindManage [put]
func (BindApi *UserBindManageApi) UpdateUserBindManage(c *gin.Context) {
	var Bind user.UserBindManage
	err := c.ShouldBindJSON(&Bind)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	if err := BindService.UpdateUserBindManage(Bind); err != nil {
		global.GVA_LOG.Error("更新失敗!", zap.Error(err))
		response.FailWithMessage("更新失敗", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindUserBindManage 用id査詢用户绑定健管师
// @Tags UserBindManage
// @Summary 用id査詢用户绑定健管师
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query user.UserBindManage true "用id査詢用户绑定健管师"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"査詢成功"}"
// @Router /Bind/findUserBindManage [get]
func (BindApi *UserBindManageApi) FindUserBindManage(c *gin.Context) {
	id := c.Query("ID")
	if reBind, err := BindService.GetUserBindManage(id); err != nil {
		global.GVA_LOG.Error("査詢失敗!", zap.Error(err))
		response.FailWithMessage("査詢失敗", c)
	} else {
		response.OkWithData(gin.H{"reBind": reBind}, c)
	}
}

// GetUserBindManageList 分页獲取用户绑定健管师列表
// @Tags UserBindManage
// @Summary 分页獲取用户绑定健管师列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query userReq.UserBindManageSearch true "分页獲取用户绑定健管师列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"獲取成功"}"
// @Router /Bind/getUserBindManageList [get]
func (BindApi *UserBindManageApi) GetUserBindManageList(c *gin.Context) {
	var pageInfo userReq.UserBindManageSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := BindService.GetUserBindManageInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage("獲取失敗", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "獲取成功", c)
	}
}
